VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Connect"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Attribute VB_Description = "Property Code Builder"
Option Explicit

Implements IDTExtensibility

' the VBIDE instance connected to this addin
Private VBInstance As VBIDE.VBE
' the new menu item added to the Add-Ins menu
Private newMenuItem As Office.CommandBarControl
' the Event object used to get a notification when
' the user clicks on newMenuItem
Private WithEvents MenuHandler As CommandBarEvents
Attribute MenuHandler.VB_VarHelpID = -1
' this is the form corresponding to this instance of the class
Dim frmAddin As New frmAddin

Private Sub IDTExtensibility_OnConnection(ByVal VBInst As Object, ByVal ConnectMode As vbext_ConnectMode, ByVal AddInInst As VBIDE.AddIn, custom() As Variant)
    
    On Error GoTo OnConnection_Error
    
    'save the VB instance
    Set VBInstance = VBInst
    
    If ConnectMode <> vbext_cm_Startup Then
        ' if no AfterStartup method will be called, this is the right
        ' place to add a menu item to the Add-Ins menu
        CreateMenuItem
    End If
    Exit Sub
    
OnConnection_Error:
    MsgBox "Unable to correctly connect the add-in", vbCritical
End Sub

Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
    ' if the add-in is being loaded at VB startup, this is
    ' the right place to add a menu item to the Add-Ins menu
    CreateMenuItem
End Sub

Private Sub IDTExtensibility_OnDisconnection(ByVal RemoveMode As vbext_DisconnectMode, custom() As Variant)
    On Error Resume Next
    
    'delete the menu item
    newMenuItem.Delete
    ' clear the menu handler object
    Set MenuHandler = Nothing
    
    ' unload the form, if necessary
    Unload frmAddin
    Set frmAddin = Nothing
End Sub

Private Sub IDTExtensibility_OnAddInsUpdate(custom() As Variant)
    '
End Sub

Sub CreateMenuItem()
    Dim addInMenu As Object
    On Error Resume Next
    
    ' search the Add-Ins menu, exit if not found
    ' (very unlikely)
    Set addInMenu = VBInstance.CommandBars("Add-Ins")
    If (Err <> 0) Or (addInMenu Is Nothing) Then Exit Sub
    
    ' add a new item to the Add-In menu, after existing ones
    Set newMenuItem = addInMenu.Controls.Add(1)
    ' set it caption to a suitable string, with a hotkey
    newMenuItem.Caption = "&Property Builder..."
    
    ' create a menu handler object that will receive
    ' a click event when the user select the menu command
    Set MenuHandler = VBInstance.Events.CommandBarEvents(newMenuItem)
    
End Sub

Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
    ' let the form know who is calling it
    Set frmAddin.Connect = Me
    Set frmAddin.VBInstance = VBInstance
    ' show it
    frmAddin.Show
End Sub



